public class OpportunitySolutionExtensionController{
    public List<SolutionComponent__c> components{get;set;}
    public List<string> componentFields{get;set;}
    public Solution__c soln {get; set;}
    
    public Boolean IsTechnology{get;set;}
    public Boolean IsConsulting{get;set;}
    public Boolean IsOutsourcing{get;set;}
    
    public Boolean disableInput{get; set;}
    public string toDelete{ get; set; }
    
    public String SolutionComponentsTitle{get; set;}
    
    public String RecType{get;set;}
    private ApexPages.StandardController solnController;
              
    public OpportunitySolutionExtensionController(ApexPages.StandardController stdController) {
        solnController = stdController;
        soln = (Solution__c)stdController.getRecord();           
    }
        
    public void Initialise(){        
        components = new List<SolutionComponent__c>();
        Schema.DescribeSObjectResult R = Solution__c.SObjectType.getDescribe();
        
        Schema.DescribeSObjectResult solcomp = SolutionComponent__c.SObjectType.getDescribe(); 
        Map<String, Schema.SObjectField> allFieldsMap = solcomp.fields.getMap();

        String SOQLSelect = '';
        for(String fldName : allFieldsMap.keySet()){
            Schema.SObjectField fld = allFieldsMap.get(fldName);
            DescribeFieldResult fieldResult = fld.getDescribe();
            string fieldName = fieldResult.getName();
            System.debug('Got field:'+fieldName);
            if(SOQLSelect != ''){
                SOQLSelect += ',' + fieldName;
            }
            else{
                SOQLSelect += 'select ' + fieldName;
            }     
        }               
        if(soln.id !=null){
            String SOQLStmt =SOQLSelect + ' from SolutionComponent__c where Solution__r.id =' + '\'' + soln.id + '\'';        
            components = Database.query(SOQLStmt);
        }
                     
        Map<ID,Schema.RecordTypeInfo> recTypesMap = R.getRecordTypeInfosByID();
        Schema.RecordTypeInfo recTypeInst = recTypesMap.get(soln.RecordTypeId);         
        RecType = recTypeInst.getName();     
        IsTechnology = (RecType == 'HR Technology');
        IsConsulting = (RecType == 'HR Consulting');
        IsOutsourcing = (RecType == 'HR Outsourcing');
        techodisable();   
    }
    
    public PageReference cancel()
    {
         PageReference p;
          p =  solnController.cancel();
         //soln = (Solution__c)solnController.getRecord(); 
         //Solution__c sol=[select id,Opportunity__r.id from Solution__c where id=:soln.Id];
         //p=new PageReference('/'+sol.Opportunity__r.id);
         return p;
    }
    public void NewSolutionComponent(){
        SolutionComponent__c solComp = new SolutionComponent__c();
        solComp.ParentType__c = soln.product__c;
        solComp.Solution__c = null;  
        components.add(solComp);
        techodisable();
    }
        
    public PageReference save() {
        Savepoint sp = Database.setSavepoint();
        PageReference retPage = null;
        try {
            retPage =  solnController.save();
            soln = (Solution__c)solnController.getRecord(); 
            for (SolutionComponent__c solComp : components){
                if(solComp.Solution__c == null)
                {
                    solComp.Solution__c = soln.Id;
                }
                Database.upsert(solComp);
            }                                  
        }
        catch (Exception e) {
            Database.rollback(sp);
            System.debug('******** exception ' + e);
        }
        return retPage; 
    }
    
    public PageReference deleteSectionItem() {
         SolutionComponent__c solComp;
         PageReference retPage = null;
      
        System.Debug('todelete'+todelete);
        if(this.toDelete != '' || this.toDelete != null){
            try{
                            
                solComp = [select id
                                 from SolutionComponent__c 
                                 where id = : this.toDelete];
                    
                delete solComp;
                            
                
                
                
            }catch(Exception e){
                System.Debug('Error while deletion:'+e);
            }
            }
             retPage =  solnController.cancel();
        
         return retPage ;
    }
    
    public void techodisable(){
        if(components.size() > 0){
            disableInput = true;
        }
        else{
            disableInput = false;
        }
    }
}